package com.ych.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ych.beans.Message;
import com.ych.beans.SensorModel;
import com.ych.dao.SensorModelDao;
import com.ych.service.SensorModelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SensorModelServiceImpl implements SensorModelService {
    @Autowired
    SensorModelDao dao;

    @Override
    public Message addSensorModel(SensorModel sensor) {
        Message message = new Message();
        boolean isSuccess = dao.addSensorModel(sensor);
        message.setSuccess(isSuccess);
        if (isSuccess){
            message.setMsg("添加成功");
        }else {
            message.setMsg("添加失败");
        }
        return message;
    }

    @Override
    public Message updateSensorModel(SensorModel sensor) {
        Message message = new Message();
        boolean isSuccess = dao.updateSensorModel(sensor);
        message.setSuccess(isSuccess);
        if (isSuccess){
            message.setMsg("修改成功");
        }else {
            message.setMsg("修改失败");
        }
        return message;
    }

    @Override
    public Message deleteSensorModel(Integer id) {
        Message message = new Message();
        boolean isSuccess = dao.deleteSensorModel(id);
        message.setSuccess(isSuccess);
        if (isSuccess){
            message.setMsg("删除成功");
        }else {
            message.setMsg("删除失败");
        }
        return message;
    }

    @Override
    public Message selectSensorModel(Integer pageNo, String key) {
        Message message = new Message();
        if(pageNo!=null){
            PageHelper.startPage(pageNo, 8);
        }
        List<SensorModel> list=dao.selectSensorModel(key);
        final PageInfo<SensorModel> objectPageInfo = new PageInfo<>(list);
        message.setSuccess(true);
        if(!list.isEmpty()){
            message.setObj(objectPageInfo);
        }
        return message;
    }
}
